package com.second.hand.trading.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.second.hand.trading.server.dao.WebSocketDao;
import com.second.hand.trading.server.service.WebSocketService;
import com.second.hand.trading.server.wb.pojo.Friend;
import com.second.hand.trading.server.wb.pojo.Message;
import com.second.hand.trading.server.wb.pojo.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WebSocketServiceImpl implements WebSocketService {

    @Autowired
    WebSocketDao webSocketDao;

    /*@Autowired
    WebSocketService webSocketService;

    //存储对话
    @Override
    public void MessageUserLog(String username, String tousername) {
        webSocketService.MessageUserLog(username,tousername);
    }

    //存储聊天信息
    @Override
    public void MessageLog(String username, String tousername, String message) {
        webSocketService.MessageLog(username,tousername,message);
    }

    //查询聊天对话
    @Override
    public List<Message> Allmessage(String username, String tousername) {
        return webSocketService.Allmessage(username,tousername);
    }

    //查询头像
    @Override
    public String Avatar(String username) {
        System.out.println("ffff");
        return webSocketService.Avatar(username);
    }*/


    //历史记录和头像
    public ResultMessage resultMessage(String username, String tousername) {
        System.out.println("ccc");
        ResultMessage resultMessage = new ResultMessage();
        System.out.println("aaaa");
        String Avatar = webSocketDao.Avatar(username);
        String toAvatar = webSocketDao.Avatar(tousername);
        List<Message> messages = webSocketDao.Allmessage(username,tousername);
        resultMessage.setUsername(username);
        resultMessage.setAvatar(Avatar);
        resultMessage.setToAvatar(toAvatar);
        resultMessage.setMessages(messages);

        System.out.println(resultMessage);

        return resultMessage;
    }


    //好友列表
    public List<Friend> Friend(String userID) {
        List<Friend> friends = new ArrayList<>();
        List<String> friend = webSocketDao.Friend(userID);
        for (String s : friend) {
            Friend friend1 = new Friend();
            friend1.setUserID(s);
            s = "\""+s+"\"";
            friend1.setAvatar(webSocketDao.Avatar(s));
            friend1.setUsername(webSocketDao.Friendname(s));
            friend1.setLastmessage(webSocketDao.lastmessage(userID,s));
            friends.add(friend1);
        }
        return friends;
    }


    public void messageStorage(String username, String tousername, String message) {
//        String username1="\""+username+"\"";
//        String tousername1="\""+tousername+"\"";
        int num = webSocketDao.num(username, tousername);
        if (num == 0) {
            webSocketDao.MessageUserLog(username, tousername);
        }

        webSocketDao.MessageLog(username, tousername, message);
    }
}
